 $(function(){
	
	easyloader.base = '../../js/tools/locale/';
    easyloader.load('easyui-lang-zh_CN.js', function(){     
	     var dateTime=getDateTime();
	    var pram={title:'',currentPage:1,startDate:"",endDate:"",categoryId:'',cityId:-1};
		
		  $.getJSON("../../groupbyajax/groupby!getSelectList.action",{flag:'1'},function(data){
					  	    $("#category").html(data.select);
					  });
					  $.getJSON("../../groupbyajax/dic!getDredgeCitysOption.action",function(data){
					  	    $("#cityId").html(data.success);
					  });
         $("#dataGrid").datagrid({
				        height: 450,
						width:1140,
                        pagination: true,
						rownumbers:true,
						striped:true,
						pageList:[15,20,25,30],
						pageSize:15,
						showPageList:true,
						singleSelect:true,
						queryParams:pram,
                        url: '../../groupbyajax/groupby!getGroupbyList.action',
						onLoadSuccess:function(data)
						{
							 $("#dataGrid tbody tr").find("td:eq(3)").dblclick(function(){
					  	         var category=$("#category").clone();
								 var name=$.trim($(this).children().html());
								 $(this).empty();
								 $(this).append(category);
								 var obj=$(this).children().find("option[text='"+name+"']").attr("selected", true);
								 $(this).children().change(function(){
								 	var categoryId=$(this).val();
									var id =$(this).parent().parent().find("td:eq(0) input").attr("value");
									
				                         $.post("../../groupbyajax/groupby!updateGroupby.action",{id:id,categoryId:categoryId}, function(data){
													    if(data.success)
														{
															 $("#dataGrid").datagrid('reload');
														     $.messager.alert("消息框","更新成功！");
														}else
														{
															$.messager.alert("消息框","更新失败！");
														}
												  });
							           
								 });
					          });
						},
                        columns: [[
						          {field: 'checkbox',title: '<input type="checkbox" name="all" onclick="selectAll(this)">',width: 30},
						          {field: 'title',title: '标题',width: 420,align: 'left'},
						          {field: 'city',title: '所属城市',width: 80,align: 'center'},
						          {field: 'category',title: '商品分类',width: 120,align: 'left'}, 
						          {field: 'price',title: '团购价',width: 80,align: 'center'}, 
								  {field: 'startTime',title: '团购开始时间',width:120,align: 'center'},
						          {field: 'endTime',title: '团购结束时间',width:120,align: 'center'},
							      {field: 'delete',title: '删除',width: 80,align: 'center'}
							 ]]
			        });		
					
					 
			});	
			
	  
});	
function addButton()
{
	
var pager = $('#dataGrid').datagrid('getPager');   
					pager.pagination({
						pageList:[15,20,25,30],
						pageSize:15,
						showPageList:true,
						buttons:[{
							iconCls:'icon-print',
							handler:function(){
								doPrint();
							}
						}]
					});
}
function search()
{
	            var title=$.trim($('#title').val());
				var startdate=$.trim($("#startdate").val());
				var enddate=$.trim($("#enddate").val());
				var categoryId=$.trim($("#category").val());
				var cityId=$("#cityId").val();
				if(cityId=="")
				{
					cityId=-1;
				}
				$('#dataGrid').datagrid({
				  striped: true,
				  queryParams:{title:title,startDate:startdate,endDate:enddate,categoryId:categoryId,cityId:cityId}
			   });
}
function deleteGroupby(id)
{
	if(id=="all")
	{
		id=getSelectIds();
	}
	
	$.messager.confirm('删除', '你确定要删除记录吗?', function(r){
		                          if(r)
								  {
								  	  $.post("../../groupbyajax/groupby!delete.action", {id:id}, function(data){
													    if(data.success)
														{
															 $("#dataGrid").datagrid('reload');
														   $.messager.alert("消息框","删除成功！");
														}else
														{
															$.messager.alert("消息框","删除失败！");
														}
													  });
								  }

							});
							
}
function selectAll(obj)
{
	if($(obj).attr("checked"))
	{
		
		$("#dataGrid tbody tr").find('td:eq(0) div input').each(function(){
			$(this).attr("checked",true);
		});
	}else
	{
		$("#dataGrid tbody tr").find('td:eq(0) div input').each(function(){
			$(this).attr("checked",false);
		});
	}
}
function getSelectIds()
{
	var ids="";
	$("#dataGrid tbody tr").find('td:eq(0) div input').each(function(){
			if($(this).attr("checked"))
			{
				ids+=$(this).val()+",";
			}
		});
		ids=ids.substring(0,ids.length-1);
		
		return ids;
}

